package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsReceiveMessage;
import com.caiyi.financial.nirvana.sms.querybean.QueryReceiveMessage;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsReceiveMessageMapper extends BaseMapper<SmsReceiveMessage> {

    @SelectProvider(type = ReceiveMessageProvider.class, method = "selectReceiveMessageListByParams")
    List<SmsReceiveMessage> getReceiveMessageListByParams(QueryReceiveMessage bean);

    class ReceiveMessageProvider {

        public String selectReceiveMessageListByParams(QueryReceiveMessage bean) {

            return new SQL() {
                {
                    SELECT("id,message_id,access_number,account,phone,content,create_time,update_time");
                    FROM("sms_receive_message");
                    if (StringUtils.isNotBlank(bean.getAccount())) {
                        WHERE("account LIKE concat('%',#{account},'%')");
                    }
                    if (StringUtils.isNotBlank(bean.getMessageId())) {
                        WHERE("message_id=#{messageId}");
                    }
                    if (StringUtils.isNotBlank(bean.getAccessNumber())) {
                        WHERE("access_number=#{accessNumber}");
                    }
                    if (StringUtils.isNotBlank(bean.getPhone())) {
                        WHERE("phone=#{phone}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time < date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }
}
